﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Kokomo.Mirror.PeFormat
{
	public class ExportedFunction
	{
		public ExportedFunction(
			uint address,
			int ordinal
			)
		{
			this.Rva = address;
			this.Ordinal = ordinal;
		}
		public ExportedFunction(
			uint rva,
			int ordinal,
			string name,
			string forwardModuleName,
			string forwardFunctionName
			)
			: this(rva, ordinal)
		{
			this.DecoratedName = name;
			this.ForwardModuleName = forwardModuleName;
			this.ForwardFunctionName = forwardFunctionName;
		}

		public override string ToString()
		{
			string text;
			if (this.ForwardFunctionName != null)
			{
				text = string.Format("Address=0x{0:X8}, Ordinal={1}, Name={2} => {3}.{4}", this.Rva, this.Ordinal, this.DecoratedName, this.ForwardModuleName, this.ForwardFunctionName);
			}
			else
			{
				text = string.Format("Address=0x{0:X8}, Ordinal={1}, Name={2}", this.Rva, this.Ordinal, this.DecoratedName);
			}
			return text;
		}

		[TypeConverter(typeof(HexConverter))]
		public uint Rva { get; private set; }

		[DisplayName("Decorated name")]
		public string DecoratedName { get; private set; }

		[DisplayName("Undecorated name")]
		public string UndecoratedName
		{
			get
			{
				if (this.DecoratedName == null) return null;
				return DebugHelper.UndecorateSymbolName(this.DecoratedName);
			}
		}

		public int Ordinal { get; private set; }

		public bool IsForwarded { get { return this.ForwardFunctionName != null; } }

		public string ForwardModuleName { get; private set; }
		public string ForwardFunctionName { get; private set; }
	}
}
